<template>
	<view class="content fofa poRel">
		<tabnav title="员工管理" :col="back"></tabnav>
		<view class="dis disAl disJuC wid100 bacFFF" style="height: 145rpx;">
			<view class="wid90 borRad10 dis disAl" style="height: 71rpx;background: #F4F3F3;">
				<view class="dis disAl disJuC hei100" style="width: 10%;">
					<image src="https://www.sign.run/applet/static/wenjian/suosou.png" style="width: 31rpx;height: 31rpx;"></image>
				</view>
				<input class="wid80" v-model="suosou" @input="suosouInput" placeholder="搜索部门、员工名称"/>
				<view v-if="suosou" @click="qingchu" class="dis disAl disJuC hei100" style="width: 10%;">
					<uni-icons type="clear" size="30"></uni-icons>
				</view>
			</view>
		</view>
		<view class="dis disAl disJuC" style="background-color: #F4F3F3;height: 71rpx;">
			<view class="wid90 foSi25" :style="'color: '+back+';'">{{gs}}
				<span class="mar-left20" style="color: #000;" v-if="staffTrue">共{{list.length}}人</span>	
			</view>
		</view>
		<scroll-view :style="'height: calc(100vh - 145rpx - 71rpx - 90rpx - '+tops+'px);'"
			@refresherrefresh="onPulling" :refresher-triggered="triggered"
			scroll-y="true" :refresher-enabled="true">
			<view class="wid100" v-if="list.length == 0 && staffTrue">
				<view class="wid100" style="height: 220rpx;"></view>
				<view class="wid100 textCen">
					<image src="https://www.sign.run/applet/static/wenjian/wushuju.png" style="width: 306rpx;height: 200rpx;"></image>
					<view class="mar-top30 foSi25" style="color: #858585;">暂无员工</view>
				</view>
			</view>
			<view v-if="list.length != 0 && !staffTrue" class="wh100 bacFFF">
				<view @click="gsClick(item)" style="height: 120rpx;"
				class="wid90 mar dis disAl" v-for="(item,index) in list" :key="index">
					<image src="https://www.sign.run/applet/static/staff/gongsAvater.png" style="width: 80rpx;height: 80rpx;"></image>
					<view class="foSi30 mar-left30 ">{{item.name}}</view>
				</view>
			</view>
			<view v-if="list.length != 0 && staffTrue" @click="tiaozhuan('/pages/staff/particulars')"
			class="borRad10" v-for="(item,index) in namejson" :key="index">
				<view v-for="(item1,index1) in item" :key="index1">
					<view v-if="index1 == 0" class="wid100 dis disAl hei60" style="background: #EEECEE;">
						<view class="foSi30 fowe600 wid90 mar">{{item1.first}}</view>
					</view>
					<view class="bacFFF">
						<view class="wid90 mar dis disAl" style="height: 147rpx;">
							<view class="dis disJuC" style="width: 20%;">
								<image v-if="!item1.avater" src="https://www.sign.run/applet/static/role/avater.png" style="width: 80rpx;height: 80rpx;border-radius: 50%;"></image>
								<image v-if="item1.avater" :src="item1.avater" style="width: 80rpx;height: 80rpx;border-radius: 50%;"></image>
							</view>
							<view class="wid80 hei80 dis disAl disJuB">
								<view class="foSi30">
									<view class="dis disAl">
										<view>{{item1.name}}</view>
										<view class="dis disAl disJuC foSi20 mar-left20" v-if="item1.zTrue"
										:style="'color: '+back+';'" style="width: 60rpx;height: 30rpx;background-color: #F4F3F3;">在职</view>
									</view>
									<view class="mar-top20" style="color: #858484;">{{item1.tel}}</view>
								</view>
								<view class="overflowHidden foSi25" style="width: 45%;color: #858484;">{{item1.quanxian}}</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view style="height: 140rpx"></view>
		</scroll-view>
		<view class="wid100 poAbs bacFFF dis disAl disJuC" style="height: 125rpx;bottom: 0;">
			<view class="hei80 dis disAl disJuC coFFF borRad10" @click="toggle('bottom')"
				style="width: 400rpx;" :style="{'background-color':back}">添加员工</view>
		</view>
		<uni-popup ref="popup" background-color="#fff">
			<view style="height: 20rpx;"></view>
			<view class="hei80 dis disAl disJuC poRel foSi35 fowe600">添加员工须知</view>
			<view style="height: 20rpx;"></view>
			<view class="foSi30 wid90 mar" style="line-height: 41rpx;color: #858484;">企业员工，具备代表企业发起合同、使用企业印章签署合同等能力（企业印章需要企业法定代表人/企业超管/印章管理员进行授权）。</view>
			<view class="xia mar-top40"></view>
			<view class="wid100 dis disAl" style="height: 125rpx;">
				<view class="wid90 mar dis disAl disJuB foSi35">
					<view class="hei80 dis disAl disJuC coFFF borRad10" :style="'background-color: '+backTwo+';color: '+back+';'" @click="guanbi"
						style="width: 300rpx;">取消</view>
					<view class="hei80 dis disAl disJuC coFFF borRad10" :style="'background-color: '+back+';'"
					 style="width: 300rpx;">确定</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import tabnav from "../common/tabnav/tabnav.vue"
	export default {
		components: {
			tabnav,
		},
		data() {
			return {
				back: uni.getStorageSync('back'),
				backTwo: uni.getStorageSync('backTwo'),
				suosou: '',
				timer: null,
				tops: 0,
				triggered: false,
				_freshing: false,
				gs: '',
				list: [],
				staffTrue: false,
				firstPin :["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z"],
				namejson:{},//转换为拼音后的数据
			}
		},
		onLoad() {
			this.listClic()
		},
		created() {
			var that = this
			uni.getSystemInfo({
				success: (e) => {
					console.log(e)
					that.tops = e.statusBarHeight
				}
			})
		},
		methods: {
			toggle(type) {
				// open 方法传入参数 等同在 uni-popup 组件上绑定 type属性
				this.$refs.popup.open(type)
			},
			guanbi(){
				this.$refs.popup.close()
			},
			tiaozhuan(url){
				uni.navigateTo({
					url: url
				});
			},
			qingchu(){
				var that = this
				if (that.timer !== null) clearTimeout(that.timer)
				that.suosou = ''
				that.list = []
				if(that.staffTrue){
					that.namejsonClick()
				}else{
					that.listClic()
				}
			},
			suosouInput: function(event) {
				var that = this
				if (that.timer !== null) clearTimeout(that.timer)
				that.timer = setTimeout(() => {
					that.suosou = event.target.value
					console.log(that.suosou)
					that.list = []
					if(that.staffTrue){
						that.namejsonClick()
					}else{
						that.listClic()
					}
				}, 1000)
			},
			onPulling(e) {
				var that = this
				if (that._freshing) return;
				that._freshing = true;
				that.triggered = true
				console.log("onpulling", e);
				setTimeout(() => {
					that.triggered = false;
					that._freshing = false;
					that.list = []
					if(that.staffTrue){
						that.namejsonClick()
					}else{
						that.listClic()
					}
				}, 3000)
			},
			gsClick(item){
				var that = this
				that.gs = item.name
				that.staffTrue = true
				that.list = []
				that.namejsonClick()
			},
			listClic(){
				var that = this
				that.list = [
					{name: '长春市千羽科技有限公司'},
					{name: '长春市千羽科技'}
				]
				that.gs = that.list[0].name
				that.staffTrue = false
			},
			namejsonClick(){
				var that = this
				let nameArr = [];
				that.list = that.list.concat([
					{avater: '',quanxian:'IT信息管理员、企业模、起草合同、合同文件夹、合同文件夹、合同文件夹、合同文件夹、合同文件夹',
					name:"张三",tel:'158****4444',zTrue: true},
					{avater: 'https://img.zcool.cn/community/0104c15cd45b49a80121416816f1ec.jpg@1280w_1l_2o_100sh.jpg',quanxian:'合同文件夹、合同文件夹、合同文件夹、合同文件夹',
					name:"李四",tel:'133****9999',zTrue: false},
					{avater: '',quanxian:'合同类型管理、合同文件夹、起草合同、合同文件夹、合同文件夹、合同文件夹、合同文件夹、合同文件夹',
					name:"王五",tel:'133****9999',zTrue: true},
					{avater: '',quanxian:'合同类型管理、合同文件夹、起草合同、合同文件夹、合同文件夹、合同文件夹、合同文件夹、合同文件夹',
					name:"王1",tel:'133****9999',zTrue: true},
				])
				let pinyin = require('js-pinyin')
				pinyin.setOptions({checkPolyphone: false, charCase: 0})
				//循环数组，拿到所有的姓名
				for(var i = 0 ; i < that.list.length ; i ++){
					// 取所有姓名的首字母
					let peopleName = that.list[i].name
					// 取所有姓名的首字母
					let firstname = pinyin.getCamelChars(peopleName).substring(0, 1)
					//给原json添加首字母键值对
					that.list[i].first = firstname
					//放入新数组
					nameArr.push(that.list[i])
					// that.result.push(res.data.content[i].id)
				}
				let namejson = {};
				//根据首字母键值对给原数据按首字母分类
				for (let i = 0; i < that.firstPin.length; i++) {    //这里的FirstPin是一个写入了所有字母的数组,见data中
					namejson[that.firstPin[i]] = nameArr.filter(function (value) {
						return value.first === that.firstPin[i]
					})
				}
				that.namejson = namejson
				console.log(that.namejson)
			},
		}
	}
</script>

<style>
	.content {
		height: 100vh;
		background-color: #F4F3F3;
	}
	.foSi28{
		font-size: 28rpx;
	}
	.xia{
		height: 1rpx;
		background: #BFBFBF;
	}
	.wid85{
		width: 85%;
	}
</style>
